package com.xhh.nphm.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhh.nphm.common.core.domain.Ztree;
import com.xhh.nphm.common.utils.StringUtils;
import com.xhh.nphm.system.domain.BSort;
import com.xhh.nphm.system.mapper.SortMapper;
import com.xhh.nphm.system.service.ISortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SortServiceImpl extends ServiceImpl<SortMapper, BSort> implements ISortService {
    @Autowired
    private SortMapper sortMapper;
    /**
     * 内容分类列表数据
     * @param bSort
     * @return
     */
    @Override
    public List<BSort> contentSortJson(BSort bSort) {
       return sortMapper.contentSortJson(bSort);
    }

    /**
     * 根据codepk 查询内容分类
     * @param codepk
     * @return
     */
    @Override
    public BSort selectByCodePk(String codepk) {
        return sortMapper.selectByCodePk(codepk);
    }

    /**
     * 校验内容名称
     * @param bSort
     * @return
     */
    @Override
    public String checkSortNameUnique(BSort bSort) {
        String deptId = StringUtils.isNull(bSort.getCodepk()) ? "-1" : bSort.getCodepk();
        QueryWrapper<BSort> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SortName",bSort.getSortname()).eq("CodeParent",bSort.getCodeparent());
        List<BSort> info = sortMapper.selectList(queryWrapper);
        if (info!=null&&info.size()>1) {
            return "1";
        }
        return "0";
    }

    /**
     * 查询对应 内容分类
     * @param codepk
     * @return
     */
    @Override
    public BSort selectSortById(String codepk) {
        QueryWrapper<BSort> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CodePK",codepk)
                .gt("CodePK",">11")
                .eq("isDel",0)
                .eq("UseFlag",1).eq("EndFlag",0);
        return sortMapper.contentSortJson(new BSort()) ==null || sortMapper.contentSortJson(new BSort()).size()==0?null : sortMapper.contentSortJson(new BSort()).get(0);
//        return sortMapper.selectList(queryWrapper)==null||sortMapper.selectList(queryWrapper).size()==0 ?  null : sortMapper.selectList(queryWrapper).get(0);
    }
    /**
     * 加载内容分类列表树（排除下级）
     */
    @Override
    public List<Ztree> selectSortTreeExcludeChild(BSort bSort) {
        List<BSort> deptList = sortMapper.contentSortJson(bSort);
        List<Ztree> ztrees = initZtree(deptList);
        return ztrees;
    }
    /**
     * 加载内容分类列表树
     */
    @Override
    public List<Ztree> selectSortTree(BSort bSort) {
        List<BSort> deptList = sortMapper.contentSortJson(bSort);
        List<Ztree> ztrees = initZtree(deptList);
        return ztrees;
    }

    @Override
    public int selectSortCount(String codepk) {
        BSort sort = new BSort();
        sort.setCodeparent(codepk);
        QueryWrapper<BSort> queryWrapper = new QueryWrapper<>();
        if(sort.getCodepk()!=null)
            queryWrapper.eq("CodePK",sort.getCodepk());
        if(sort.getCodeparent()!=null)
            queryWrapper.eq("CodeParent",sort.getCodeparent());
        return sortMapper.selectCount(queryWrapper);
    }

    @Override
    public int deleteByCodePk(String codepk) {
        return sortMapper.deleteByCodePk(codepk);
    }

    /**
     * 对象转部门树
     *
     * @param deptList 部门列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<BSort> deptList) {
        return initZtree(deptList, null);
    }
    /**
     * 对象转部门树
     *
     * @param sortList     内容列表
     * @param roleDeptList 角色已存在菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<BSort> sortList, List<String> roleDeptList) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(roleDeptList);
        for (BSort sort : sortList) {
                Ztree ztree = new Ztree();
                ztree.setId(Long.parseLong(sort.getCodepk()));
                ztree.setpId(Long.parseLong(sort.getCodeparent()==null ? "0" : sort.getCodeparent()));
                ztree.setName(sort.getSortname());
                ztree.setTitle(sort.getSortname());
                if (isCheck) {
                    ztree.setChecked(roleDeptList.contains(sort.getCodepk() + sort.getSortname()));
                }
                ztrees.add(ztree);
        }
        return ztrees;
    }

}
